package com.ctaa.ctaa.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class QQMailSender {
    // 收件人邮箱
    public static Boolean sendEmail(String recipientEmail,String newPassword) {

        // 发件人邮箱和授权码
        final String senderEmail = "1047267211@qq.com";
        final String authCode = "uofqbcahngptbdje"; // QQ邮箱的授权码，非邮箱密码

        // 邮件主题和内容
        String subject = "找回密码";
        String content = recipientEmail + "密码重置成功，新密码：" + newPassword;

        // 配置邮件服务器属性
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", "587");

        // 创建会话对象并进行认证
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(senderEmail, authCode);
            }
        });

        try {
            // 创建邮件对象
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(senderEmail, "记账宝"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
            message.setSubject(subject);
            message.setText(content);
            // 发送邮件
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
        return false;
    }

    /**
     * 生成密码
     */
}
